#!/usr/bin/env python

# --------------------------------------
# projects/python/collatz/TestCollatz.py
# Copyright (C) 2009
# --------------------------------------

# To run the tests
#     TestCollatz.py

# To document the tests
#     pydoc -w TestCollatz

import main
import unittest

# ----------
# TestReader
# ----------

class TestReader (object) :
    def __init__ (self, s) :
        self.s = s

    def read (self) :
        return self.s
	
# ----------
# TestWriter
# ----------

class TestWriter (object) :
    def str (self) :
        return self.s

    def write (self, *a) :
        self.s = str(a[0])
        for w in a[1:] :
            self.s += ' '
            self.s += str(w)
        self.s += '\n'


# -----------
# TestCollatz
# -----------

class TestCollatz (unittest.TestCase) :
   
   
    #----------
    # isTie()
    #----------
    def test_isTie1(self):
	main.all_candidates = ['q','w','e']
	main.winners = []
	main.num_votes = [10,10, 0]
	losersballots = []
	self.assert_(main.isTie(losersballots) == True)

    def test_isTie2(self):
        main.all_candidates = ['q','w','e']
        main.winners = []
        main.num_votes = [10,10, 1]
        losersballots = [[3,2,1]]
        self.assert_(main.isTie(losersballots) == False)
        

    #----------
    # winner_exist
    #----------
    def test_winner_exist1(self):
	main.all_candidates = ['q','w']
	main.ballots = []
	main.winner_exist()
	self.assert_(main.winners == ['q', 'w'])

    def test_winner_exist2(self):
        main.all_candidates = ['q','w','z']
        main.ballots = [[1,2,3],[3,2,1],[2,1,3],[2,1,3],[3,2,1]]
	main.num_votes = [1,2,2]
	main.winners = []
	print main.winner_exist(), " SSSSSSSSS"
        self.assert_(main.winner_exist() == False)

    def test_winner_exist3(self):
        main.all_candidates = ['t','y','z']
	main.ballots = [[1,2,3],[3,2,1],[2,1,3],[3,1,2],[2,3,1],[3,2,1],[3,1,2],[3,1,2]]
        main.num_votes = [1,2,5]
	main.winners = []
	self.assert_(main.winner_exist() == True)  

    #----------- 
    # findLosers
    #----------
    def test_findLosers1(self):
	main.ballots = [[1,2,3],[3,2,1],[2,1,3],[3,1,2],[2,3,1],[3,2,1]]       
	main.lists = [[[1,2,3]],[[2,1,3],[2,3,1]],[[3,2,1],[3,1,2],[3,2,1]]]
	main.num_votes = [1,2,3]
        self.assert_( main.findLosers(main.lists) == [[1,2,3],[2,1,3],[2,3,1]])

    # ----
    # eval
    # ----
    def test_eval1(self):
	main.num_cases = 1
	main.num_candidates = 2
	main.all_candidates = ['q','w']   
	main.ballots = [[1,2],[1,2],[2,1],[2,1]]
        main.my_eval()
        self.assert_(main.winners == ['q','w'])

    def test_eval2(self):
        main.num_cases = 1
        main.num_candidates = 2
        main.all_candidates = ['q','w']
        main.ballots = [[1,2],[2,1],[2,1]]
        main.my_eval()
        self.assert_(main.winners == ['w'])

    def test_eval3(self):
        main.num_cases = 1
        main.num_candidates = 3
        main.all_candidates = ['q','w','e']
        main.ballots = [[1,2,3],[3,2,1],[2,1,3],[3,1,2],[2,3,1],[3,2,1]]
        main.my_eval()
        self.assert_(main.winners == ['e'])

if __name__ == "__main__" :
    unittest.main()

